package com.sxz.sbs.module.client;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.IntentionStallAdapter;
import com.sxz.sbs.module.client.bean.IntentionStallBean;
import com.sxz.sbs.module.client.presenter.IntentionStallPresenter;
import com.sxz.sbs.module.client.view.IntentionStallView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/11
 * 意向车位（关联车位）
 */

public class IntentionStallActivity extends BaseActivity<IntentionStallPresenter> implements IntentionStallView {

    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.iv_title_standby)
    ImageView ivTitleRight;
    private IntentionStallAdapter intentionStallAdapterl;
    private List<IntentionStallBean> mlist = new ArrayList<>();

    private int parkingId;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("意向车位").showRightReadyImg(R.drawable.ic_adds).build().show();
        parkingId = getIntent().getIntExtra("parking_id",0);
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getIntentionStallList();
            }
        });
        refreshLayout.setRefreshing(true);
        intentionStallAdapterl=new IntentionStallAdapter(mlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(intentionStallAdapterl);
        /**
         * 取消意向车位的监听
         */
        intentionStallAdapterl.setOnCanceClickListener(new IntentionStallAdapter.OnCancelClickListener() {
            @Override
            public void onCancelClick(int i) {
                getCancelIntentionStall(i);
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getIntentionStallList();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_intentionstall;
    }

    @OnClick({R.id.iv_title_standby})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_standby:
                ARouter.getInstance().build(RouterUrl.SALE_CONTROL_STALL)
                        .withString(IntentConstant.SALE_CONTROL_BUILD_NAME,"选择意向车位")
                        .withInt(IntentConstant.SALE_CONTROL_BUILD_ID,parkingId)
                        .withInt("from",1)
                        .withInt("customId",getIntent().getIntExtra(IntentConstant.CUSTOM_ID, 0))
                        .navigation();
               /* Intent intent=new Intent(IntentionStallActivity.this,AllHousesActivity.class);
                intent.putExtra(IntentConstant.STALL_HOUSES,"stall");
                startActivity(intent);*/
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getIntentionStallList();
    }

    @Override
    protected IntentionStallPresenter createPresenter() {
        return new IntentionStallPresenter();
    }

    /**
     * 获取意向车位列表
     * @param data
     */
    @Override
    public void onIntentionStallSuccess(List<IntentionStallBean> data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            stateLayout.showContent();
            if (data.size() > 0) {
                mlist.clear();
                mlist.addAll(data);
                intentionStallAdapterl.notifyDataSetChanged();
                intentionStallAdapterl.loadMoreComplete();
            } else {
                stateLayout.showEmpty();
                mlist.clear();
                intentionStallAdapterl.notifyDataSetChanged();
            }
        }
    }

    /**
     * 取消意向车位
     * @param data
     */
    @Override
    public void onCancelIntentionStallSuccess(String data) {
        getIntentionStallList();

    }

    /**
     * 获取意向车位列表
     */
    private void getIntentionStallList() {
        /**
         customId	int	是	客户Id
         */
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("customId", getIntent().getIntExtra(IntentConstant.CUSTOM_ID, 0));
        mPresenter.getIntentionStall(map);
    }
    /**
     * 取消意向车位
     */
    private void getCancelIntentionStall(int i) {
        /**
         customSpaceId	int	是	意向关联表ID
         */
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("customSpaceId", mlist.get(i).getCustomspaceId());
        mPresenter.getCancelIntentionStall(map);
    }

}
